﻿using System;
using System.Data;
using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
//using Coolite.Ext.Web;

namespace LongNet.Util
{
    public class HRPage : System.Web.UI.Page
    {
        public UserEntityInfo UserEntityInfo { set; get; }
        /// <summary>
        /// 是否登陆
        /// </summary>
        public bool IsLogin { set; get; }


        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);
        }
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            try
            {
                UserEntityInfo = HttpContext.Current.Session["UserEntityInfo"] as UserEntityInfo;
                if (UserEntityInfo != null)
                {
                    IsLogin = true;
                }
                else
                {
                    IsLogin = false;
                    UserEntityInfo = new UserEntityInfo();
                    //Ext.Redirect("../Login.aspx", "登陆失效！");
                    return;
                }
            }
            catch (Exception ex)
            {
                //Ext.Msg.Alert("HRPage", ex.Message).Show();
            }
        }

        public  bool ValidateUrl()
        {
            try
            {
            string BuildingId = Util.CommonClass.GetStringQueryString("BuildingId");
            string BuildingIds = Util.CommonClass.GetStringQueryString("BuildingIds");
            string HouseId = Util.CommonClass.GetStringQueryString("HouseId");
            string HouseIds = Util.CommonClass.GetStringQueryString("HouseIds");
            string CommunityId = Util.CommonClass.GetStringQueryString("CommunityId");
            string CommunityIds = Util.CommonClass.GetStringQueryString("CommunityIds");
            string OwnersCommitteeId = Util.CommonClass.GetStringQueryString("OwnersCommitteeId");
            string OwnersCommitteeIds = Util.CommonClass.GetStringQueryString("OwnersCommitteeIds");
            string strSql = "";
            switch (this.UserEntityInfo.ModuleId)
            {
                case 1:  //州端口
                   
                    return true;
                case 2:  //县市登陆
                    //验证单个楼栋id
                    if (!string.IsNullOrEmpty(BuildingId.Trim()))
                    {
                        strSql = " Select distinct  cityId From dbo.VW_Building where id in(" + BuildingId + ")";
                        object obj = SqlClass.GetObject(strSql);
                        if (obj != null)
                        {
                            int cityid = Convert.ToInt32(obj);
                            if (cityid != 0 && cityid != this.UserEntityInfo.CityId)
                            {
                                return false;
                            }
                        }
                    }

                    //验证楼栋ids
                    if (!string.IsNullOrEmpty(BuildingIds.Trim()))
                    {
                        strSql = " Select distinct  cityId From dbo.VW_Building where id in(" + BuildingIds.Trim(',') + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0)
                        {
                            if (rs.DataResult.Tables[0].Rows.Count > 1 || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0][0]) != this.UserEntityInfo.CityId)
                            {
                                return false;
                            }
                        }

                    }

                    //验证单个房屋id
                    if (!string.IsNullOrEmpty(HouseId.Trim()))
                    {
                        strSql = " Select distinct  cityId From VW_House where id in(" + HouseId + ")";
                        object obj = SqlClass.GetObject(strSql);
                        if (obj != null)
                        {
                            int cityid = Convert.ToInt32(obj);
                            if (cityid != 0 && cityid != this.UserEntityInfo.CityId)
                            {
                                return false;
                            }
                        }
                    }

                    //验证房屋ids
                    if (!string.IsNullOrEmpty(HouseIds.Trim()))
                    {
                        strSql = " Select distinct  cityId From VW_House where id in(" + HouseIds.Trim(',') + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0)
                        {
                            if (rs.DataResult.Tables[0].Rows.Count > 1 || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0][0]) != this.UserEntityInfo.CityId)
                            {
                                return false;
                            }
                        }

                    }

                    //验证单个小区id
                    if (!string.IsNullOrEmpty(CommunityId.Trim()))
                    {
                        strSql = " Select distinct  cityId From VW_Community where id in(" + CommunityId + ")";
                        object obj = SqlClass.GetObject(strSql);
                        if (obj != null)
                        {
                            int cityid = Convert.ToInt32(obj);
                            if (cityid != 0 && cityid != this.UserEntityInfo.CityId)
                            {
                                return false;
                            }
                        }
                    }

                    //验证小区ids
                    if (!string.IsNullOrEmpty(CommunityIds.Trim()))
                    {
                        strSql = " Select distinct  cityId From VW_Community where id in(" + CommunityIds.Trim(',') + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0)
                        {
                            if (rs.DataResult.Tables[0].Rows.Count > 1 || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0][0]) != this.UserEntityInfo.CityId)
                            {
                                return false;
                            }
                        }

                    }


                    //验证单个物业id
                    if (!string.IsNullOrEmpty(OwnersCommitteeId.Trim()))
                    {
                        strSql = " Select distinct  cityId From VW_OwnersCommittee where id in(" + OwnersCommitteeId + ")";
                        object obj = SqlClass.GetObject(strSql);
                        if (obj != null)
                        {
                            int cityid = Convert.ToInt32(obj);
                            if (cityid != 0 && cityid != this.UserEntityInfo.CityId)
                            {
                                return false;
                            }
                        }
                    }

                    //验证小区ids
                    if (!string.IsNullOrEmpty(OwnersCommitteeId.Trim()))
                    {
                        strSql = " Select distinct  cityId From VW_OwnersCommittee where id in(" + OwnersCommitteeIds.Trim(',') + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0)
                        {
                            if (rs.DataResult.Tables[0].Rows.Count > 1 || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0][0]) != this.UserEntityInfo.CityId)
                            {
                                return false;
                            }
                        }

                    }  
                    break;
                case 3:   //银行端口
                case 4:   //财政端口
                case 5:   //开发商端口
                case 6:   //物业端口
                case 7:    //小区端口
                    return true;
                case 8:    //业主委员会端口

                    //验证单个楼栋id
                    if (!string.IsNullOrEmpty(BuildingId.Trim()))
                    {
                        strSql = " Select   cityId,CommunityId From dbo.VW_Building where id in(" + BuildingId + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count>0)
                        {
                            DataRow dr = rs.DataResult.Tables[0].Rows[0];
                            int cityid = PublicClass.GetInt32Value(dr["CityId"]);
                            int communityId = PublicClass.GetInt32Value(dr["CommunityId"]);
                            if (cityid != this.UserEntityInfo.CityId||communityId!=this.UserEntityInfo.CommunityId)
                            {
                                return false;
                            }
                        }
                    }

                    //验证楼栋ids
                    if (!string.IsNullOrEmpty(BuildingIds.Trim()))
                    {
                        strSql = " Select distinct  cityId,CommunityId From dbo.VW_Building where id in(" + BuildingIds.Trim(',') + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0)
                        {
                            if (rs.DataResult.Tables[0].Rows.Count > 1 || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0]["CityId"]) != this.UserEntityInfo.CityId || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0]["CommunityId"]) != this.UserEntityInfo.CommunityId)
                            {
                                return false;
                            }
                        }

                    }

                    //验证单个房屋id
                    if (!string.IsNullOrEmpty(HouseId.Trim()))
                    {
                        strSql = " Select   cityId,CommunityId From VW_House where id in(" + HouseId + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count > 0)
                        {
                            DataRow dr = rs.DataResult.Tables[0].Rows[0];
                            int cityid = PublicClass.GetInt32Value(dr["CityId"]);
                            int communityId = PublicClass.GetInt32Value(dr["CommunityId"]);
                            if (cityid != this.UserEntityInfo.CityId || communityId != this.UserEntityInfo.CommunityId)
                            {
                                return false;
                            }
                        }
                    }

                    //验证房屋ids
                    if (!string.IsNullOrEmpty(HouseIds.Trim()))
                    {
                        strSql = " Select  cityId,CommunityId From VW_House where id in(" + HouseIds.Trim(',') + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0)
                        {
                            if (rs.DataResult.Tables[0].Rows.Count > 1 || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0]["CityId"]) != this.UserEntityInfo.CityId || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0]["CommunityId"]) != this.UserEntityInfo.CommunityId)
                            {
                                return false;
                            }
                        }

                    }

                    //验证单个小区id
                    if (!string.IsNullOrEmpty(CommunityId.Trim()))
                    {
                        strSql = " Select  cityId,CommunityId From VW_Community where id in(" + CommunityId + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count > 0)
                        {
                            DataRow dr = rs.DataResult.Tables[0].Rows[0];
                            int cityid = PublicClass.GetInt32Value(dr["CityId"]);
                            int communityId = PublicClass.GetInt32Value(dr["CommunityId"]);
                            if (cityid != this.UserEntityInfo.CityId || communityId != this.UserEntityInfo.CommunityId)
                            {
                                return false;
                            }
                        }
                    }

                    //验证小区ids
                    if (!string.IsNullOrEmpty(CommunityIds.Trim()))
                    {
                        strSql = " Select  cityId,Id From VW_Community where id in(" + CommunityIds.Trim(',') + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0)
                        {
                            if (rs.DataResult.Tables[0].Rows.Count > 1 || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0]["CityId"]) != this.UserEntityInfo.CityId || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0]["Id"]) != this.UserEntityInfo.CommunityId)
                            {
                                return false;
                            }
                        }

                    }


                    //验证单个物业id
                    if (!string.IsNullOrEmpty(OwnersCommitteeId.Trim()))
                    {
                        strSql = " Select  cityId,CommunityId From VW_OwnersCommittee where id in(" + OwnersCommitteeId + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count > 0)
                        {
                            DataRow dr = rs.DataResult.Tables[0].Rows[0];
                            int cityid = PublicClass.GetInt32Value(dr["CityId"]);
                            int communityId = PublicClass.GetInt32Value(dr["CommunityId"]);
                            if (cityid != this.UserEntityInfo.CityId || communityId != this.UserEntityInfo.CommunityId)
                            {
                                return false;
                            }
                        }
                    }

                    //验证小区ids
                    if (!string.IsNullOrEmpty(OwnersCommitteeId.Trim()))
                    {
                        strSql = " Select cityId,CommunityId From VW_OwnersCommittee where id in(" + OwnersCommitteeIds.Trim(',') + ")";
                        Result rs = SqlClass.GetDataSet(strSql);
                        if (rs.IsSuccess && rs.DataResult != null && rs.DataResult.Tables.Count > 0)
                        {
                            if (rs.DataResult.Tables[0].Rows.Count > 1 || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0]["CityId"]) != this.UserEntityInfo.CityId || PublicClass.GetInt32Value(rs.DataResult.Tables[0].Rows[0]["CommunityId"]) != this.UserEntityInfo.CommunityId)
                            {
                                return false;
                            }
                        }

                    }                 
                    return true;
                default:
                    return false;
            }
            }
            catch (Exception ex)
            {
                //Ext.Msg.Alert("HRPage", ex.Message).Show();
                return false;
            }
            return true;

        }
        public  object[] GetPostValues(string GridPanel, string fieldName)
        {
            object[] objs = null;
            List<object> list = new List<object>();
            try
            {
                bool hasGrid = false;
                string formKeysName = "";
                foreach (var item in this.Request.Form)
                {
                    if (item.ToString().IndexOf(GridPanel) != -1)
                    {
                        hasGrid = true;
                        formKeysName = item.ToString();
                        break;
                    }
                }
                if (hasGrid)
                {
                    DataTable dt = Util.DataCopy.ConvertJsonToDataTale(this.Request.Form.GetValues(formKeysName)[0]);
                    if(dt.Columns.Contains(fieldName))
                    {
                        list = Util.DataConvert.GetColumnDataToList<object>(dt, fieldName);
                        objs=new object[list.Count];
                        list.CopyTo(objs);
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
                
            }
            catch (Exception ex)
            {
                //Ext.Msg.Alert("异常", ex.Message).Show();
            }
            finally
            {
                
            }
            return objs;
           
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                //UserEntityInfo = HttpContext.Current.Session["UserEntityInfo"] as UserEntityInfo;
                //if (UserEntityInfo != null)
                //{
                //    IsLogin = true;
                //}
                //else
                //{
                //    IsLogin = false;
                //    UserEntityInfo = new UserEntityInfo();
                //    Ext.Redirect("../Login.aspx", "登陆失效！");
                //    return;
                //}
                if (!IsLogin)
                {
                    //Ext.Redirect("../index.html", "登陆失效！");
                    return;
                }
                if (!ValidateUrl())
                {
                    //Ext.Redirect("../index.html", "禁止访问！");
                    return;
                }
            }
            catch (Exception ex)
            {
                //Ext.Msg.Alert("HRPage", ex.Message).Show();
            }


        }
        public string EnCodeToBase64(string text)
        {
            byte[] byte_text = System.Text.Encoding.GetEncoding("GB2312").GetBytes(text);
            return Convert.ToBase64String(byte_text);
        }
        public string GetQueryString(string key)
        {
            string strQuery = "";
            try
            {
                string text = Util.CommonClass.GetStringQueryString(key);
                byte[] byte_text = Convert.FromBase64String(text);
                strQuery = System.Text.Encoding.GetEncoding("GB2312").GetString(byte_text);               
            }
            catch    
            {
                strQuery = "";
            }
            return strQuery;
        }
    }
    [Serializable]
    public class UserEntityInfo
    {
        public UserEntityInfo()
        {
            RoleId = new List<int>();
            RoleName = new List<string>();
        }
        /// <summary>
        /// 用户Id
        /// </summary>
        public int UserId { set; get; }
        /// <summary>
        /// 用户登陆账号
        /// </summary>
        public string UserName { set; get; }
        /// <summary>
        /// 用户全称
        /// </summary>
        public string FullName { set; get; }

        /// <summary>
        /// 当前模块Id
        /// </summary>
        public int ModuleId { set; get; }

        /// <summary>
        /// 实体表名
        /// </summary>
        public string EntityTableName { set; get; }
        /// <summary>
        /// 实体表Id
        /// </summary>
        public int EntityTableId { set; get; }

        /// <summary>
        /// 角色Id
        /// </summary>
        public List<int> RoleId { set; get; }
        /// <summary>
        /// 角色信息
        /// </summary>
        public List<string> RoleName { set; get; }


        #region 地域Id
        /// <summary>
        /// 州Id
        /// </summary>
        public int ProvinceId { set; get; }

        /// <summary>
        /// 县市Id
        /// </summary>
        public int CityId { set; get; }

        /// <summary>
        /// 管辖的城市Ids
        /// </summary>
        public string CityIds { set; get; }

        /// <summary>
        /// 银行Id
        /// </summary>
        public int BankInfoId { set; get; }

        /// <summary>
        /// 财政Id
        /// </summary>
        public int FinanceId { set; get; }

        /// <summary>
        /// 开发商Id
        /// </summary>
        public int DevCompanyId { set; get; }



        /// <summary>
        /// 街道办Id
        /// </summary>
        public int VillageId { set; get; }

        /// <summary>
        /// 小区Id
        /// </summary>
        public int CommunityId { set; get; }

        /// <summary>
        /// 业主委员会Id
        /// </summary>
        public int OwnersCommitteeId { set; get; }

        /// <summary>
        /// 物业Id
        /// </summary>
        public int PropertyDeptId { set; get; }
        #endregion
        #region 身份状态
        /// <summary>
        /// 是否是州用户
        /// </summary>
        public bool IsProvince { set; get; }
        /// <summary>
        /// 是否是县市用户
        /// </summary>
        public bool IsCity { set; get; }

        /// <summary>
        /// 是否是银行用户
        /// </summary>
        public bool IsBankInfo { set; get; }
        /// <summary>
        /// 是否是财政用户
        /// </summary>
        public bool IsFinance { set; get; }
        /// <summary>
        /// 是否是开发商用户
        /// </summary>
        public bool IsDevcompany { set; get; }
        /// <summary>
        /// 是否是物业用户
        /// </summary>
        public bool IsPropertyDept { set; get; }

        /// <summary>
        /// 是否小区委员会用户
        /// </summary>
        public bool IsOwnersCommittee { set; get; }

        /// <summary>
        /// 是否小区用户
        /// </summary>
        public bool IsCommunity { set; get; }
        public override string ToString()
        {
            string userinfo = string.Format("用户名:{0},当前模块Id:{1},EntityTableName :{2},EntityTableId:{3},CityId {4},RoleIds:{5}", UserName, ModuleId, EntityTableName, EntityTableId, CityId,DataConvert.ChangeListToString(RoleId,","));
            return userinfo;
        }
        #endregion
    }

    public class DepositedChargeInfo
    {
        /// <summary>
        /// 缴存类型
        /// </summary>
        public DepositedType Type { get; set; }
        /// <summary>
        /// 缴存金额
        /// </summary>
        public float DepositedMoney { get; set; }
        /// <summary>
        /// 缴存公式
        /// </summary>
        public string DepositedFunction { get; set; }
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; }

    }
    public enum DepositedType
    {
        /// <summary>
        /// 按房款百分比
        /// </summary>
        TotalPrice = 0,
        /// <summary>
        /// 按建筑结构面积百分比
        /// </summary>
        totalArea = 1
    }
}
